package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.ppl.BallEliminate;
import com.yanqu.road.entity.activity.ppl.PplDrumReward;
import com.yanqu.road.entity.activity.ppl.shootlogic.PplScreenPos;
import com.yanqu.road.entity.activity.yingxin.YingXinUserData;
import com.yanqu.road.entity.activity.yingxin.config.HotPotFloorConfig;
import com.yanqu.road.entity.activity.yingxin.config.YingXinConfig;
import com.yanqu.road.entity.activity.yingxin.floor.YingXinFloorCellData;
import com.yanqu.road.entity.activity.yingxin.floor.YingXinFloorData;
import com.yanqu.road.entity.activity.yingxin.game.YingXinGameData;
import com.yanqu.road.entity.activity.yingxin.game.YingXinLine;
import com.yanqu.road.pb.activity.YingXinProto;
import com.yanqu.road.server.manger.activity.yingxin.YingXinGameLogic;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class YingXinPb {

    public static YingXinProto.YingXinEnergyMsg.Builder parseYingXinEnergyMsg(YingXinUserData userData) {
        YingXinProto.YingXinEnergyMsg.Builder builder = YingXinProto.YingXinEnergyMsg.newBuilder();
        builder.setEnergy(userData.getEnergy());
        builder.setLastRecoveryTime(userData.getLastRecTime());
        return builder;
    }

    public static YingXinProto.YingXinGameDataMsg.Builder parseYingXinGameDataMsg(YingXinUserData userData, int multi) {
        YingXinProto.YingXinGameDataMsg.Builder builder = YingXinProto.YingXinGameDataMsg.newBuilder();

        // 每行数据
        YingXinGameData gameData = userData.getGameDataMap().get(multi);
        if (gameData == null) {
            gameData = new YingXinGameData();
        }
        for (YingXinLine line : gameData.getLines()) {
            builder.addLineList(parseYingXinLineDataMsg(line));
        }

        builder.setCurColor(gameData.getCur());
        for (int count : userData.getCollectList().get(multi)) {
            builder.addHotPotList(count);
        }
        builder.setGameOver(YingXinGameLogic.isGameOver(gameData));
        builder.setMultiType(multi);
        builder.setEnergyMsg(parseYingXinEnergyMsg(userData));
        builder.setScore(userData.getScore());
        return builder;
    }

    public static YingXinProto.YingXinLineDataMsg.Builder parseYingXinLineDataMsg(YingXinLine line) {
        YingXinProto.YingXinLineDataMsg.Builder builder = YingXinProto.YingXinLineDataMsg.newBuilder();
        builder.setY(line.getY());
        for (int ball : line.getBalls()) {
            builder.addColor(ball);
        }
        return builder;
    }

    public static YingXinProto.YingXinHotPotDataMsg.Builder parseYingXinHotPotDataMsg(int type, YingXinFloorData floorData,int gotBigReward) {
        YingXinProto.YingXinHotPotDataMsg.Builder builder = YingXinProto.YingXinHotPotDataMsg.newBuilder();
        builder.setType(type);
        builder.setFloor(floorData.getFloor());
        for (YingXinFloorCellData cellData : floorData.getCellList()) {
            if (cellData.isOpen()) {
                builder.addRewardList(cellData.getReward());
            } else {
                builder.addRewardList("");
            }
        }
        builder.setGotBigReward(gotBigReward);
        return builder;
    }

    public static List<YingXinProto.YingXinScreenPosMsg> parseYingXinScreenPosMsgList(List<PplScreenPos> screenPosList) {
        List<YingXinProto.YingXinScreenPosMsg> screenPosMsgList = new ArrayList<>();
        for (PplScreenPos screenPos : screenPosList) {
            YingXinProto.YingXinScreenPosMsg.Builder builder = YingXinProto.YingXinScreenPosMsg.newBuilder();
            builder.setScreenX(screenPos.getScreenX().intValue());
            builder.setScreenY(screenPos.getScreenY().intValue());
            screenPosMsgList.add(builder.build());
        }
        return screenPosMsgList;
    }

    public static List<YingXinProto.YingXinDropBallMsg> parseYingXinDropBallMsgList(List<BallEliminate> eliminateList) {
        List<YingXinProto.YingXinDropBallMsg> eliminateBallMsgList = new ArrayList<>();
        for (BallEliminate ball : eliminateList) {
            YingXinProto.YingXinDropBallMsg.Builder eliminateBallMsg = YingXinProto.YingXinDropBallMsg.newBuilder();
            eliminateBallMsg.setX(ball.getX());
            eliminateBallMsg.setY(ball.getY());
            eliminateBallMsg.setDrumIndex(ball.getDrumIndex());
            if (!StringUtils.isNullOrEmpty(ball.getReward())) {
                eliminateBallMsg.setReward(ball.getReward());
            }
            eliminateBallMsgList.add(eliminateBallMsg.build());
        }
        return eliminateBallMsgList;
    }

    public static List<YingXinProto.YingXinHotPotRewardMsg> parseYingXinHotPotRewardMsgList(List<PplDrumReward> hotPotRewardList) {
        List<YingXinProto.YingXinHotPotRewardMsg> rewardMsgList = new ArrayList<>();
        for (PplDrumReward hotPotReward : hotPotRewardList) {
            YingXinProto.YingXinHotPotRewardMsg.Builder rewardMsg = YingXinProto.YingXinHotPotRewardMsg.newBuilder();
            rewardMsg.setIndex(hotPotReward.getIndex());
            rewardMsg.setReward(hotPotReward.getReward());
            rewardMsgList.add(rewardMsg.build());
        }
        return rewardMsgList;
    }

    public static YingXinProto.YingXinArrayPosMsg parseYingXinArrayPosMsg(int x, int y) {
        YingXinProto.YingXinArrayPosMsg.Builder builder = YingXinProto.YingXinArrayPosMsg.newBuilder();
        builder.setX(x);
        builder.setY(y);
        return builder.build();
    }

    public static YingXinProto.YingXinSyncConfigRespMsg.Builder parseYingXinSyncConfigRespMsg(YingXinConfig config) {
        YingXinProto.YingXinSyncConfigRespMsg.Builder builder = YingXinProto.YingXinSyncConfigRespMsg.newBuilder();
        for (Map<Integer, HotPotFloorConfig> map : config.getFloorMap().values()) {
            for (HotPotFloorConfig floorConfig : map.values()) {
                YingXinProto.YingXinHotPotConfigMsg.Builder tmpBuilder = YingXinProto.YingXinHotPotConfigMsg.newBuilder();
                tmpBuilder.setType(floorConfig.getType());
                tmpBuilder.setFloor(floorConfig.getFloor());
                tmpBuilder.setConsume(floorConfig.getConsume());
                tmpBuilder.setSpecReward(floorConfig.getSpecReward());
                tmpBuilder.addAllRewardList(floorConfig.getRewardList());
                builder.addHotPotList(tmpBuilder);
            }
        }
        return builder;
    }
}
